<?php


namespace BoojasrMVC\Controller;


use BoojasrFramework\Controller\AbstractController;
use BoojasrFramework\Obs\Observable;
use BoojasrFramework\Obs\Observer;
use BoojasrFramework\Obs\ObserverInterface;
use BoojasrFramework\Response\Response;
use BoojasrFramework\Service\DoctrineManageService;
use BoojasrFramework\ServicesContainer\Request;
use BoojasrFramework\ServicesContainer\RequestStack;
use BoojasrMVC\AppServicesFactory;
use BoojasrMVC\Entity\User;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/")
 */
class HelloWorldController extends AbstractController
{
    public function helloWorld(Request $request,RequestStack $requestStack,$id){
        $twig = AppServicesFactory::$container->get("twig");
        $helloSevice = AppServicesFactory::$container->get("hello_service");
        /** @var DoctrineManageService $doctrine */
        $doctrine = AppServicesFactory::$container->get("doctrine");
        $em = $doctrine->getEntityManage();
        $userRepository = $em ->getRepository(User::class);
        $userFind = $userRepository->findOneBy(["username"=>"J A R M I N G"]);
        return $this->view("@App/hello.html.twig",[]);
    }

    /**
     * @Route("/hello_world2")
     */
    public function helloWorld2(){
        return new Response("hello world!!!");
    }

    /**
     * @Route("/hello/obs")
     */
    public function helloWorldObs(){
        $user = new User();
        $user->setUsername("小明");
        $obs = new Observable();
        $obs->addObserver(new class implements ObserverInterface{
            public function receiveNotify($value)
            {
                echo print_r($value,true);
            }

            public function getName()
            {
                return "hello_observer";
            }
        });
        $obs->user = $user;
        $user->setUsername("老唐");
        $obs->notify();
        return new Response("hello_world obs!!!");
    }
}
